var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/widgets/AutoResize.tsx
var AutoResize_exports = {};
__export(AutoResize_exports, {
  AutoResize: () => AutoResize,
  polyfillResize: () => polyfillResize
});
module.exports = __toCommonJS(AutoResize_exports);
var import_react = __toESM(require("react"));
var import_util = require("../util");
var globalWindow = window;
function AutoResize({ children, style, onResize, beforeResize, delay = void 0 }) {
  const objRef = (0, import_react.useRef)();
  const mountRef = (0, import_react.useRef)(false);
  const beforeResizeRef = (0, import_react.useRef)(beforeResize);
  beforeResizeRef.current = beforeResize;
  const supportResizeObserver = globalWindow.ResizeObserver;
  (0, import_react.useLayoutEffect)(() => {
    mountRef.current = true;
    if (onResize) {
      const lastBcr = { width: 0, height: 0 };
      const pEl = objRef.current.parentElement;
      const offset = pEl ? {
        x: (parseFloat(import_util.iup.getStyle(pEl, "paddingLeft")) || 0) + (parseFloat(import_util.iup.getStyle(pEl, "paddingRight")) || 0),
        y: (parseFloat(import_util.iup.getStyle(pEl, "paddingTop")) || 0) + (parseFloat(import_util.iup.getStyle(pEl, "paddingBottom")) || 0)
      } : { x: 0, y: 0 };
      const onResizeEvent = (options = {}) => {
        if (!mountRef.current || !objRef.current || !objRef.current.offsetParent) {
          return;
        }
        const bcr = {
          width: objRef.current.clientWidth,
          height: objRef.current.clientHeight
        };
        if (bcr.width !== lastBcr.width || bcr.height !== lastBcr.height) {
          lastBcr.width = bcr.width;
          lastBcr.height = bcr.height;
          onResize(
            {
              width: bcr.width - offset.x,
              height: bcr.height - offset.y
            },
            options
          );
        }
      };
      const debounce = (func, delayTime) => {
        let timer = null;
        let exec = delayTime ? setTimeout : window["requestIdleCallback"] || window["requestAnimationFrame"];
        let cancel = delayTime ? clearTimeout : window["cancelIdleCallback"] || window["cancelAnimationFrame"];
        return () => {
          timer && cancel(timer);
          !timer && func({ flushSync: true });
          timer = exec(() => {
            timer = null;
            func();
          }, delayTime || void 0);
        };
      };
      let resizeObserver;
      let targetView;
      const debounceFn = debounce(onResizeEvent, delay ?? 60);
      const resize = () => {
        var _a;
        if (!mountRef.current || !objRef.current) {
          return;
        }
        (_a = beforeResizeRef.current) == null ? void 0 : _a.call(beforeResizeRef, {
          width: objRef.current.clientWidth,
          height: objRef.current.clientHeight
        });
        debounceFn();
      };
      if (supportResizeObserver) {
        resizeObserver = new supportResizeObserver(resize);
        resizeObserver.observe(objRef.current);
      } else {
        targetView = objRef.current.contentWindow;
        if (targetView) {
          targetView.addEventListener("resize", resize);
        } else {
          setTimeout(() => {
            var _a, _b;
            (_b = (_a = objRef.current) == null ? void 0 : _a.contentWindow) == null ? void 0 : _b.addEventListener("resize", resize);
          });
        }
      }
      if (objRef.current.clientWidth > 0) {
        onResizeEvent();
      } else if (!supportResizeObserver) {
        setTimeout(() => {
          objRef.current.clientWidth > 0 && onResizeEvent();
        });
      }
      return () => {
        var _a, _b;
        mountRef.current = false;
        if (resizeObserver) {
          resizeObserver.disconnect();
          resizeObserver = null;
        } else {
          (_b = (_a = objRef.current) == null ? void 0 : _a.contentWindow) == null ? void 0 : _b.removeEventListener("resize", resize);
        }
      };
    }
    return () => {
      mountRef.current = false;
    };
  }, [onResize]);
  const props = supportResizeObserver ? {} : {
    type: "text/html",
    data: "about:blank"
  };
  return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, children, onResize && import_react.default.createElement(supportResizeObserver ? "div" : "iframe", {
    ref: objRef,
    tabIndex: -1,
    style: {
      display: "block",
      position: "absolute",
      top: 0,
      left: 0,
      height: "100%",
      width: "100%",
      overflow: "hidden",
      opacity: 0,
      pointerEvents: "none",
      zIndex: -1,
      ...style
    },
    ...props
  }));
}
function polyfillResize() {
  const supportResizeObserver = globalWindow.ResizeObserver;
  if (!supportResizeObserver) {
    window.dispatchEvent(new Event("resize"));
  }
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  AutoResize,
  polyfillResize
});
